package pms.Biz.impl;

import pms.Biz.MedicineBiz;
import pms.dao.MedicineDao;
import pms.dao.impl.MedicineDaoImpl;
import pms.entity.Medicine;

import java.time.LocalDate;
import java.util.List;

public class MedicineBizImpl implements MedicineBiz {

    private MedicineDao dao=new MedicineDaoImpl();


    @Override
    public List<Medicine> qurryMedicineNumberByName(String name) {
        return dao.qurryMedicineNumberByName(name);
    }

    @Override
    public List<Medicine> checkAllMedicineNumberUnderThd() {
        return dao.qurryAllMedicineNumberUnderThd();
    }

    @Override
    public List<Medicine> checkMedicineDateBeformNow() {
        return dao.qurryAllMedicineDateBeforeNow();
    }

    @Override
    public List<Medicine> checkNearTermMedicine() {
        return dao.qurryAllNearTermMedicine();
    }

    @Override
    public void WarnMedicine() {
        if(dao.qurryNearTermMedicine()){
            System.out.println("存在近效期药品，请尽快处理");
        }
        if(dao.qurryMedicineDateBeforeNow()){
            System.out.println("存在过期药品，请尽快处理");
        }
        if(dao.qurryMedicineNumberUnderThd()){
            System.out.println("存在数量低于阈值的药品，请尽快处理");
        }
    }

    @Override
    public boolean CleanUselessRecord() {
        if (dao.deleteMedicileDateBeforeNow()||dao.deleteMedicineNumberEqual0()){
            return true;
        }
        return false;
    }

    @Override
    public boolean addMedicine(Medicine medicine) {
        return dao.addMedicine(medicine);
    }

    @Override
    public boolean changeNumber(String name, LocalDate date, int number) {
        return dao.updateMedicineNumberByNameAndDate(name,date,number);
    }

    @Override
    public boolean changePrice(String name, double price) {
        return dao.updateMedicinePriceByName(name,price);
    }
    @Override
    public Medicine qurryNumber(Integer id) {
        return dao.qurryMedicineNumberByID(id);
    }

    @Override
    public List<Medicine> qurryMedicine(String medicinename) {
        return dao.queryMedicineByMedicinenameForLeave(medicinename);
    }

    @Override
    public List<Medicine> qurryThd(Integer id) {
        return dao.qurryMedicineByThd();
    }

    @Override
    public Medicine qurryMedicine(Integer id) {
        return dao.qurryMedicineByID(id);
    }

    @Override
    public boolean upMedicineNumber(int medicineid,int medicinenumber) {
        if (dao.updateMedicineNumberForUp(medicineid,medicinenumber)) {
            //如果有则修改成功
            Medicine medicine=dao.qurryMedicineByID(medicineid);
            medicine.setMedicine_number(medicine.getMedicine_number()+medicinenumber);
            return true;
        } else {
            System.out.println("药品数量修改失败");
            return false;
        }
    }

    @Override
    public boolean downMedicineNumber(int medicineid,int medicinenumber) {
        if (dao.updateMedicineNumberForDown(medicineid,medicinenumber)) {
            //如果有则修改成功
            Medicine medicine=dao.qurryMedicineByID(medicineid);
            medicine.setMedicine_number(medicine.getMedicine_number()-medicinenumber);
            return true;
        } else {
            System.out.println("药品数量修改失败");
            return false;
        }
    }
}
